$(function () {
    init_cusSale();
    $("body").on('click','[data-stopPropagation]',function (e) {e.stopPropagation();});
    /**意向等级**/
    $(".multiple-select2").select2({placeholder:"选择意向客户"});
    $(".multiple-select2").on("change", function(e) {
        var yixiang = $("#bpGroud").val();
        var yArray = new Array();
        $.each(yixiang,function (index,val) {yArray.push(val);});
        var conver = yArray.join(",");
        $("#bpgroupSelect").val(conver);
        $JuXue.refreshTable("bigDisTable");
    });
    /**导出次数**/
    $(".multiple-select3").select2({placeholder:"选择导出次数"});
    $(".multiple-select3").on("change", function(e) {
        var empNum = $("#bpEpNum").val();
        var yArray = new Array();
        $.each(empNum,function (index,val) {yArray.push(val);});
        var conver = yArray.join(",");
        $("#bpnumSelect").val(conver);
        $JuXue.refreshTable("bigDisTable");
    });
    /**学历层次*/
    $(".multiple-select4").select2({placeholder:"选择学历层次"});
    $(".multiple-select4").on("change", function(e) {
        var bpEducation = $("#bpEducation").val();
        $("#educationSelect").val(bpEducation);
        $JuXue.refreshTable("bigDisTable");
    });

    /**是否分配*/
    $(".multiple-select5").select2({placeholder:"选择是否分配"});
    $(".multiple-select5").on("change", function(e) {
        var distruteNum = $("#distruteNum").val();
        $("#distrutSelect").val(distruteNum);
        $JuXue.refreshTable("bigDisTable");
    });

    /**部门*/
    $(".multiple-select6").select2({placeholder:"选择所属部门"});
    $.getJSON(ctx+"dept/list",{},function (r) {
        $("#bpDeptId").empty().append("<option value=\"\">请选择部门</option>").append("<option value=\"\"></option>");
        $.each(r,function (index,value) {
            var deptid = value.deptId;
            var deptname = value.deptName;
            $("#bpDeptId").append("<option value="+deptid+">"+deptname+"</option>");
        })
    });
    $(".multiple-select6").on("change", function(e) {
        var bpDeptId = $("#bpDeptId").val();
        $("#deptHid").val(bpDeptId);
        $JuXue.refreshTable("bigDisTable");
        changeDirecter(bpDeptId);
    });


    $(".multiple-select7").select2({placeholder:"选择跟进人"});
    $(".multiple-select7").on("change", function(e) {
        var bpDistribute = $("#bpDistribute").val();
        $("#distributeHid").val(bpDistribute);
        $JuXue.refreshTable("bigDisTable");
    });

    $('#collapseTwo').collapse({toggle: false})
    $("#collapseThree").collapse({toggle: false});
    $("#collapseOne").collapse({toggle: false});
    $("#collapsecishu").collapse({toggle: false});
    /**高校**/
    $.getJSON(ctx+"collage/getCollageList",{},function (r) {
        var collageArray = new Array();
        $.each(r,function (index,value) {
            collageArray.push(value.collageName);
        });
        $( "#collage-autocomplete" ).autocomplete({
            source: collageArray,
            select: function( event, ui ) {
                var selectvalue = ui.item.value;
                $("#collage-autocomplete").val(selectvalue);
                $JuXue.refreshTable("bigDisTable");
                $.getJSON(ctx+"collage/getCollagePref",{"collageName":selectvalue},function (r) {
                    $("#collagePref").empty().append("<option value=\"\">请选择学院</option>").append("<option value=\"\"></option>");
                    $.each(r,function (index,value) {
                        $("#collagePref").append("<option value="+value.collageId+">"+value.collageName+"</option>");
                    })
                });
            }
        });
    });
    /**导入批次**/
    $.getJSON(ctx+"bigEpExport/getlimitBigRecords",{},function (r) {
        var recodsArray = new Set();
        $.each(r,function (index,value) {
            recodsArray.add(value.epBatch);
        });
        $("#bpBatch").autocomplete({
            source: Array.from(recodsArray),
            select: function( event, ui ) {
                $("#bpBatch").val(ui.item.label);
                $JuXue.refreshTable("bigDisTable");
            }
        });
    });

    $("#advance-daterange span").html(moment().subtract("days",29).format("YYYY-MM-DD")+" - "+moment().format("YYYY-MM-DD"));

    $('#advance-daterange').daterangepicker({
        timePicker: true, //显示时间
        timePicker24Hour: true, //时间制
        timePickerSeconds: true, //时间显示到秒
        startDate: moment().hours(0).minutes(0).seconds(0), //设置开始日期
        endDate: moment(new Date()), //设置结束器日期
        maxDate: moment(new Date()), //设置最大日期
        "opens": "center",
        ranges: {
            '今天': [moment(), moment()],'昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            '上周': [moment().subtract(6, 'days'), moment()],
            '前30天': [moment().subtract(29, 'days'), moment()],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        },
        showWeekNumbers: true,
        locale: {
            format: "YYYY-MM-DD HH:mm:ss", //设置显示格式
            applyLabel: '确定', //确定按钮文本
            cancelLabel: '取消', //取消按钮文本
            customRangeLabel: '自定义',
            daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
            monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                '七月', '八月', '九月', '十月', '十一月', '十二月'
            ],
            firstDay: 1
        },
    }).on('apply.daterangepicker', function(ev, picker) {
        var starttime = picker.startDate.format('YYYY-MM-DD');
        var endtime = picker.endDate.format('YYYY-MM-DD');
        $("#advance-daterange span").html(starttime+" - "+endtime);
        $("#starttime").val(starttime);
        $("#endtime").val(endtime);
        $JuXue.refreshTable("bigDisTable");
    });
    /**大资源批次的输入**/
    $('#bpBatch').live('input propertychange', function() {
        $JuXue.refreshTable("bigDisTable");
    });
    /**所属专员的输入**/
    $('#bpdirector').live('input propertychange', function() {
        $JuXue.refreshTable("bigDisTable");
    });
    /**高校资源**/
    $('#collage-autocomplete').live('input propertychange', function() {
        $JuXue.refreshTable("bigDisTable");
    });

});


function changeDirecter(deptid) {
    $.getJSON(ctx+"user/selectUserByDept",{deptId:deptid},function (r) {
        $("#bpDistribute").empty();
        $.each(r,function (index,val) {
            var userUuid = val.userUuid;
            var nickname = val.nickname;
            $("#bpDistribute").append("<option value="+userUuid+">"+nickname+"</option>");
        })
    })
}

/**初始化客户**/
function init_cusSale(){
    var settings = {
        url: ctx + "bigEpResoure/list",
        pageSize: 15,
        queryParams: function(params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                //筛选条件
                bpBatch:  $("#bpBatch").val(),//批次
                bpDirector :$("#bpdirector").val(),//所属专员
                bpColleges:$("#collage-autocomplete").val(), //学校
                bpAcademy: $("#selectAcademy").val(), //学院
                bpMajor  :$("#collageZhuan").val(), //专业
                bpGroud  :$("#bpgroupSelect").val(), //意向等级
                bpEpNum  :$("#bpnumSelect").val(), //导出次数
                bpResType:$("#bpResType").val(), //大资源类型
                bpInteAipro:$("#bpInteProject").val(), //意向项目
                bpSourChanel: $("#bpSOurChanel").val(), //来源渠道
                starttime :$("#starttime").val(),     //考试时间
                endtime :$("#endtime").val(),         //结束时间
                isBig : "no", //有意向没有分配的
                bpDistribute :$("#distrutSelect").val(), //分配状态
                bpDeptId :$("#deptHid").val(), //部门
                bpMyCustomerName :$("#distributeHid").val(), //跟进人
                bpEducation:$("#educationSelect").val()//学历层次
            };
        },
        columns: [{
            checkbox: true
        },
            {
                field: 'bpName',
                title: '客户名称',
                width: 150,
                clickToSelect:false,
                class:'text-center',
                formatter:function (value, row, index) {
                    return "<a href='javascript:void(0)' onclick='getCustomerDetail(this)' data-id='"+row.bpId+"'>"+value+"</a>";
                }
            }, {
                field: 'bpMobile',
                class:'text-center',
                title: '客户手机号'
            }, {
                field: 'bpColleges',
                class:'text-center',
                title: '所属院校'
            }, {
                field: 'bpMajor',
                class:'text-center',
                title: '专业'
            }, {
                field: 'bpGroud',
                class:'text-center',
                title: '意向等级'
            }, {
                field: 'bpEducation',
                class:'text-center',
                title: '学历层次',
                formatter:function (value, row, index) {
                    switch (value) {
                        case "2":return"专科";
                        case "3":return"本科";
                        case "4":return"硕士";
                        case "5":return"博士";
                    }
                }
            }, {
                field: 'bpYear',
                class:'text-center',
                title: '入学年份'
            }, {
                field: 'bpDistribute',
                class:'text-center',
                title: '是否分配',
                formatter:function (value, row, index) {
                    if(value==null||value==""){
                        return "否";
                    }else{
                        return "是";
                    }
                }
            }, {
                field: 'bpDeptName',
                class:'text-center',
                title: '当前跟进部门'
            }, {
                field: 'bpDistribute',
                class:'text-center',
                title: '当前跟进人'
            }, {
                field: 'bpCreatetime',
                class:'text-center',
                title: '创建时间',
                formatter:function (value, row, index) {
                    return value.substring(0,10);
                }
            }, {
                field: 'bpDirector',
                class:'text-center',
                title: '创建人'
            }
        ],
        onClickCell:function(field, value, row, $element){
            if(field == "bpName"){
                return ;
            }else{
                var a = ".theme-panel", b = "active";
                //$(a).hasClass(b) ? $(a).removeClass(b) : $(a).addClass(b)
                $(a).addClass(b);
                $("#selectTheme").show();
                var selected = $("#bigDisTable").bootstrapTable('getSelections');
                var selected_length = selected.length;
                $("#sutomeSelect").text(selected_length+1);
            }
        },
        onCheck:function (row,ele) {
            var selected = $("#bigDisTable").bootstrapTable('getSelections');
            var selected_length = selected.length;
            if (!selected_length) {
                var a = ".theme-panel", b = "active";
                $(a).removeClass(b);
                $("#selectTheme").hide();
            }else{
                var a = ".theme-panel", b = "active";
                $(a).addClass(b);
                $("#selectTheme").show();
            }
            $("#sutomeSelect").text(selected_length);
        },
        onUncheck:function (rows) {
            var selected = $("#bigDisTable").bootstrapTable('getSelections');
            var selected_length = selected.length;
            if (!selected_length) {
                var a = ".theme-panel", b = "active";
                $(a).removeClass(b);
                $("#selectTheme").hide();
            }else{
                var a = ".theme-panel", b = "active";
                $(a).addClass(b);
                $("#selectTheme").show();
            }
            $("#sutomeSelect").text(selected_length);
        },
        onCheckAll:function (rows) {
            var a = ".theme-panel", b = "active";
            //$(a).hasClass(b) ? $(a).removeClass(b) : $(a).addClass(b)
            $(a).addClass(b);
            $("#selectTheme").show();
            $("#sutomeSelect").text(rows.length);
        },
        onUncheckAll:function (rows) {
            var a = ".theme-panel", b = "active";
            $(a).removeClass(b);
            $("#selectTheme").hide();
        }
    };
    $JuXue.initTable('bigDisTable', settings);
    // /**客户阶段**/
    // $.post(ctx+"saleConfig/initTrackTable",function (r) {
    //     var data = r.data;
    //     var defaultbtn = '<button type="button" class="btn btn-link m-l-10 p-3 f-s-10 btn-primary" onclick="cusStage(this)" data-click="">不限</button>';
    //     $("#customeTrack").empty().append(defaultbtn);
    //     $.each(data,function (index,val) {
    //         var btn = '<button type="button" class="btn btn-link m-l-10 p-3 f-s-10" onclick="cusStage(this)" data-click="'+val.saleUuid+'">'+val.saleName+'</button>';
    //         $("#customeTrack").append(btn)
    //     });
    // });
}
